#!/bin/bash

# ZKP 修复部署脚本
# 使用方法: ./scripts/deploy_fix.sh

echo "=== ZKP 修复部署脚本 ==="
echo ""

# 1. 检查密钥文件
echo "🔑 检查密钥文件..."

# 删除客户端不需要的 verifying key
if [ -f "auth_verifying.key" ]; then
    echo "⚠️  检测到 auth_verifying.key (客户端不需要)"
    rm -f auth_verifying.key
    echo "✅ 已删除 auth_verifying.key"
fi

# 检查必需的 proving key
if [ ! -f "auth_proving.key" ]; then
    echo "❌ 错误: auth_proving.key 不存在"
    echo ""
    echo "客户端必须有 auth_proving.key 才能运行！"
    echo ""
    echo "解决方案:"
    echo "  1. 从开发团队获取统一生成的 auth_proving.key"
    echo "  2. 将文件放置在项目根目录"
    echo "  3. 重新运行此脚本"
    echo ""
    exit 1
else
    key_size=$(stat -f%z "auth_proving.key" 2>/dev/null || stat -c%s "auth_proving.key" 2>/dev/null || echo "unknown")
    echo "✅ auth_proving.key 存在 (大小: ${key_size} bytes)"
fi

echo ""

# 2. 重新编译
echo "📦 重新编译项目..."
go build -o client2 .
if [ $? -ne 0 ]; then
    echo "❌ 编译失败"
    exit 1
fi
echo "✅ 编译成功"

echo ""

# 3. 运行测试
echo "🧪 运行 ZKP 测试..."
go run ./cmd/zkp_test/main.go
if [ $? -ne 0 ]; then
    echo "❌ 测试失败"
    echo ""
    echo "📋 故障排查:"
    echo "   1. 确认 zkp/circuit.go 已更新"
    echo "   2. 确认所有旧密钥文件已删除"
    echo "   3. 查看完整错误日志"
    exit 1
fi

echo ""
echo "============================================================"
echo "✅ 修复成功! ZKP 功能正常"
echo "============================================================"
echo ""
echo "下一步:"
echo "  1. 运行主程序: ./client2"
echo "  2. 查看文档: cat ZKP_FIX_GUIDE.md"
echo ""
